
export const useWaitTimer = (user) => {
  const now = ref(Date.now());
  let timer = 0;

  const update = () => {
    now.value = Date.now();
    // 只在可见时更新
    if (document.visibilityState === 'visible') {
      timer = requestAnimationFrame(update);
    }
  };

  onMounted(() => {
    timer = requestAnimationFrame(update);
  });

  onUnmounted(() => {
    cancelAnimationFrame(timer);
  });

  // 用户等待时长计算
  const userWait = computed(() => {
    return user.value.last_user_msg 
      ? Math.floor((now.value - user.value.last_user_msg) / 1000)
      : 0;
  });

  // 客服等待时长计算
  const agentWait = computed(() => {
    return user.value.last_agent_msg 
      ? Math.floor((now.value - user.value.last_agent_msg) / 1000)
      : 0;
  });

  return { userWait, agentWait };
};